home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_084 / ed / subst.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  1KB  |  75 lines

  1. /*
  2.  * Copyright 1987 Brian Beattie Rights Reserved.
  3.  *
  4.  * Permission to copy and/or distribute granted under the
  5.  * following conditions:
  6.  *
  7.  * 1). No charge may be made other than resonable charges
  8.  *    for reproduction.
  9.  *
  10.  * 2). This notice must remain intact.
  11.  *
  12.  * 3). No further restrictions may be added.
  13.  *
  14.  */
  15. #include <stdio.h>
  16. #include "tools.h"
  17. #include "ed.h"
  18. subst(pat, sub, gflg, pflag)
  19. TOKEN    *pat;
  20. char    *sub;
  21. int    gflg, pflag;
  22. {
  23.     int    lin, chngd, nchngd;
  24.     char    *txtptr, *txt;
  25.     char    *lastm, *m, *new, buf[MAXLINE];
  26.  
  27.     if(line1 <= 0)
  28.         return( ERR );
  29.     nchngd = 0;        /* reset count of lines changed */
  30.     for(lin = line1; lin <= line2; lin++)
  31.     {
  32.         txt = txtptr = gettxt(lin);
  33.         new = buf;
  34.         chngd = 0;
  35.         lastm = NULL;
  36.         while(*txtptr)
  37.         {
  38.             if(gflg || !chngd)
  39.                 m = amatch(txtptr, pat, txt);
  40.             else
  41.                 m = NULL;
  42.             if(m != NULL && lastm != m)
  43.             {
  44.                 chngd++;
  45.                 new = catsub(txtptr, m, sub, new,
  46.                         buf+MAXLINE);
  47.                 lastm = m;
  48.             }
  49.             if(m == NULL || m == txtptr)
  50.             {
  51.                 *new++ = *txtptr++;
  52.             } else {
  53.                 txtptr = m;
  54.             }
  55.         }
  56.         if(chngd)
  57.         {
  58.             if(new >= buf+MAXLINE)
  59.                 return( ERR );
  60.             *new++ = EOS;
  61.             del(lin,lin);
  62.             ins(buf);
  63.             nchngd++;
  64.             if(pflag)
  65.                 doprnt(curln, curln);
  66.         }
  67.     }
  68.     if(nchngd == 0 && !gflg)
  69.     {
  70.         printf("string not found\n");
  71.         return(ERR);
  72.     }
  73.     return( nchngd );
  74. }
  75.